//
//  TeachKnowledgeCell.m
//  Grammar
//
//  Created by Rick on 6/29/13.
//
//

#import "TeachKnowledgeCell.h"

@implementation TeachKnowledgeCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
- (void)awakeFromNib
{
    [super awakeFromNib];
    [_contentWebView setBackgroundColor:[UIColor clearColor]];
    [_contentWebView setOpaque:NO];

    _contentWebView.scrollView.showsHorizontalScrollIndicator = TRUE;
    _contentWebView.scrollView.showsVerticalScrollIndicator = TRUE;
//    _contentWebView.scrollView.scrollEnabled = FALSE;
    
}
- (void)setCellWithKnowledge:(GrammarExplain*)knowledge WithRefresh:(BOOL)isRefrsh
{
//    if (isRefrsh) {
        
//    }
    _currentExplain = [knowledge retain];
    
    if ([_currentExplain.imageArray count] > 0) {
        _cellType = DetailKnowledgeCellTypeWithImage;
    }else {
        _cellType = DetailKnowledgeCellTypeWithOutImage;
    }
    [self layoutCellWithType:_cellType];
    if ([_currentExplain.content length] == 0) {
        _contentWebView.height = 0;
        [_contentWebView loadHTMLString:_currentExplain.content baseURL:nil];
    }else {
        [_contentWebView loadHTMLString:_currentExplain.content baseURL:nil];
    }
    
//    [_contentWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:knowledge.url]]];
    _isFinishDownloaded = isRefrsh;
    
}

- (void)layoutCellWithType:(DetailKnowledgeCellType)type
{
    switch (type) {
        case DetailKnowledgeCellTypeWithOutImage:
            _contentImageView.hidden = YES;
            _contentWebView.top = 10;
            break;
        case DetailKnowledgeCellTypeWithImage:
            _contentImageView.hidden = NO;
            [self generateImageView];
            _contentWebView.top = 60;
            break;
        default:
            break;
    }
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {

    CGRect frame = webView.frame;
    frame.size.height = 1;
    webView.frame = frame;
    CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    webView.frame = frame;
    
//    int A=  MIN([[webView stringByEvaluatingJavaScriptFromString: @"document.documentElement.clientHeight"] intValue], [[webView stringByEvaluatingJavaScriptFromString: @"document.documentElement.scrollHeight"] intValue]);
//    int B = MIN([[webView stringByEvaluatingJavaScriptFromString: @"document.body.scrollHeight"] intValue], [[webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"] intValue]);
//
//    ;
//    _contentWebView.frame = CGRectMake(0, 0, 0, 0);
//    CGRect frame = webView.frame;
//    frame.size.height = MAX(MAX(A, B), [[webView stringByEvaluatingJavaScriptFromString: @"document.documentElement.offsetHeight"] intValue]);
//    if (frame.size.height >SCREEN_HEIGHT) {
//        frame.size.height = SCREEN_HEIGHT - 20;
//    }
//    _contentWebView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
//    ITTDINFO(@"self.view height %f",self.height);
    self.height = _contentWebView.bottom;
    if (_delegate && [_delegate respondsToSelector:@selector(loadIsDoneWitHeight:)] && _isFinishDownloaded) {
        [_delegate loadIsDoneWitHeight:self.height];
    }
}

- (CGFloat)getCellHeight
{
    if (_isFinishDownloaded) {
        return self.height;
    }else {
        return 0;
    }
    
}

- (void)generateImageView
{
    CGFloat origientX = 0.0;
    
    for (NSString *imageUrl in _currentExplain.imageArray) {
        origientX += 10;
        CGRect imageViewRect = CGRectMake(origientX, 10, 40, 40);
        ITTImageView *imageView = [[ITTImageView alloc] initWithFrame:imageViewRect];
        imageView.delegate = self;
        [imageView setEnableTapEvent:YES];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [imageView loadImage:imageUrl];
        origientX += 40;
        [_contentImageView addSubview:imageView];
        [imageView release];
    }
}

- (void)imageClicked:(ITTImageView *)imageView
{
    if (_delegate && [_delegate respondsToSelector:@selector(didSelectImageView:)]) {
        [_delegate didSelectImageView:imageView.imageUrl];
    }
}

- (void)dealloc {
    [_contentWebView release];
    [_contentImageView release];
    [super dealloc];
}
@end
